<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of object
 *
 * @author pudy
 */
class Object extends CI_Controller {

    function view($poi_id) {
        /* get all poi's objects */
        $this->load->model('objectModel', 'objectModel');
        $options = array('poi_id' => $poi_id,
            'columnSelect' => 'id, path');
        $getReturnObjects = $this->objectModel->getObjects($options);
        $object_list = $getReturnObjects;
        $data['object_list'] = $object_list;
        /* end of get all poi's objects */

        $this->load->model('poiModel', 'poiModel');
        $options = array('id' => $poi_id,
            'columnSelect' => 'title');
        $getReturnPoi = $this->poiModel->getPois($options);

        $data['poi_id'] = $poi_id;
        $data['title'] = $getReturnPoi[0]->title . ' - AR Object';
        $data['main_content'] = 'object/object_list_view';

        $this->load->view('includes/template', $data);
    }

    function add($poi_id) {
        $this->load->model('objectModel', 'objectModel');
        $options = array('poi_id' => $poi_id,
            'columnSelect' => 'path');
        $getReturnObjects = $this->objectModel->getObjects($options);
        if (is_bool($getReturnObjects)) {
            $data['obj_view'] = 3;
        } else {
            if (count($getReturnObjects) == 2) {
                $data['obj_view'] = 0;
            } else {
                $file1 = 'file1';
                $path = $getReturnObjects[0]->path;
                $pos = strpos($path, $file1);
                if ($pos === false) {
                    $data['obj_view'] = 1;
                } else {
                    $data['obj_view'] = 2;
                }
            }
        }

        $this->load->model('poiModel', 'poiModel');
        $options = array('id' => $poi_id,
            'columnSelect' => 'title');
        $getReturnPoi = $this->poiModel->getPois($options);

        $data['poi_id'] = $poi_id;
        $data['title'] = $getReturnPoi[0]->title . ' - Add AR Object';
        $data['main_content'] = 'object/add_object_view';

        $this->load->view('includes/template', $data);
    }

    function add_submit($poi_id) {
        $filename = $this->input->post('filename');

        $upload_path = './res/' . $poi_id;
        if (!file_exists($upload_path)) {
            $create = mkdir($upload_path);
        }
        $config['upload_path'] = $upload_path;
        $config['allowed_types'] = 'gif|jpg|png|3gp';
        $config['file_name'] = $filename;
        
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload()) {
            $message = 'Upload gagal';
            $this->session->set_flashdata('message', $message);
            redirect('object/add/' . $poi_id, 'refresh');
        } else {
            $data = array('userfile' => $this->upload->data());
            $path = $data['userfile']['file_name'];
            
            if ($data['userfile']['is_image']) {
                $type = 1;
            } else {
                $type = 2;
            }

            $options = array(
                'poi_id' => $poi_id,
                'path' => $path,
                'type' => $type
            );
            $this->load->model('objectModel', 'objectModel');
            $getReturnInsert = $this->objectModel->addObject($options);

            $message = 'Upload berhasil';
            $this->session->set_flashdata('message', $message);
            redirect('object/add/' . $poi_id, 'refresh');
        }
    }

    function edit($object_id) {
        $this->load->model('objectModel', 'objectModel');
        $options = array('id' => $object_id,
            'columnSelect' => 'path');
        $getReturnObject = $this->objectModel->getObjects($options);

        $path = $getReturnObject[0]->path;
        $file1 = 'file1';
        $pos = strpos($path, $file1);
        if ($pos === false) {
            $data['filename'] = 'file2';
            $data['title'] = 'File 2 - Edit AR Object';
        } else {
            $data['title'] = 'File 1 - Edit AR Object';
            $data['filename'] = 'file1';
        }

        $data['object_id'] = $object_id;
        $data['main_content'] = 'object/edit_object_view';

        $this->load->view('includes/template', $data);
    }

    function edit_submit($object_id) {
        $this->load->model('objectModel', 'objectModel');
        $options = array('id' => $object_id,
            'columnSelect' => 'poi_id, path');
        $getReturnObject = $this->objectModel->getObjects($options);
        //$path = $getReturnObject[0]->path;
        $path = './res/' . $getReturnObject[0]->poi_id . '/' . $getReturnObject[0]->path;
        unlink($path);

        $poi_id = $getReturnObject[0]->poi_id;
        $filename = $this->input->post('filename');

        $upload_path = './res/' . $poi_id;
        if (!file_exists($upload_path)) {
            $create = mkdir($upload_path);
        }
        $config['upload_path'] = $upload_path;
        $config['allowed_types'] = 'gif|jpg|png|3gp';
        $config['file_name'] = $filename;

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload()) {
            $message = 'Edit gagal';
            $this->session->set_flashdata('message', $message);
            redirect('object/edit/' . $object_id, 'refresh');
        } else {
            $data = array('userfile' => $this->upload->data());
            $path = $data['userfile']['file_name'];
            //$path = $data['userfile']['full_path'];
            if ($data['userfile']['is_image']) {
                $type = 1;
            } else {
                $type = 2;
            }

            $options = array(
                'id' => $object_id,
                'path' => $path,
                'type' => $type
            );
            $getReturnUpdate = $this->objectModel->updateObject($options);

            $message = 'Edit berhasil';
            $this->session->set_flashdata('message', $message);
            redirect('object/edit/' . $object_id, 'refresh');
        }
    }

    function delete($object_id) {
        $this->load->model('objectModel', 'objectModel');
        $options = array('id' => $object_id,
            'columnSelect' => 'poi_id, path'
        );
        $getReturnObject = $this->objectModel->getObjects($options);

        $poi_id = $getReturnObject[0]->poi_id;
        //$path = $getReturnObject[0]->path;
        $path = './res/' . $poi_id . '/' . $getReturnObject[0]->path;

        $options = array('id' => $object_id);
        $getReturnDelete = $this->objectModel->deleteObject($options);
        unlink($path);
        if ($getReturnDelete) {
            $message = 'Your AR Object has been deleted.';
        } else {
            $message = 'Your AR Object can\'t be deleted.';
        }
        $this->session->set_flashdata('message', $message);
        redirect('object/view/' . $poi_id, 'refresh');
    }

}

?>
